package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*
       新增菜品
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 1 : 向菜品插入1条数据 需要返回菜品的id
        dishMapper.inster(dish);

        // 获取生成的主键值
        Long id = dish.getId();

        // 获取口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
        }

        // 向口味表插入n条数据
        dishFlavorMapper.insertBatch(flavors);
    }

    /*
      分页查询
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> p = dishMapper.page(dishPageQueryDTO);
        return new PageResult(p.getTotal(), p.getResult());
    }


    /*
      菜品删除
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            // 起售中的菜品不能删除
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 和套餐关联的菜品不能删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);

        if (setmealIds != null && setmealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        // 可以删除菜品 删除两张表 口味表 菜品表
        for (Long id : ids) {
            dishMapper.deleteId(id); // 根据主键id删除菜品表
            dishFlavorMapper.deleteByDishId(id); //根据菜品id删除口味表

        }
    }


    /*
       根据id查询回显菜品
     */
    @Override
    public DishVO getByIdDishFlavor(Long id) {
        // 根据Id查询菜品数据
        Dish dv = dishMapper.getById(id);

        // 根据id查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByIdFlavor(id);

        // 将查询的菜品数据 和 口味进行拷贝
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dv, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /*
       修改数据
     */
    @Override
    public void update(DishDTO dishDTO) {
        // 影响了 菜品表 和 口味表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品
        dishMapper.update(dish);

        // 修改口味 先删 后添加
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                // 重新设置菜品Id
                flavor.setDishId(dishDTO.getId());
            }
            // 批量插入口味数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /*
       启用禁用
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 菜品停售时 相关套餐也自动停售
        if (status == StatusConstant.DISABLE){
            // 根据菜品id 查询相关套餐
          List<Setmeal> setmeals =  setmealDishMapper.getDishIdBySermeal(id);
          if (setmeals != null && setmeals.size() > 0){
              for (Setmeal setmeal : setmeals) {
                  setmeal.setStatus(StatusConstant.DISABLE);
                  setmealMapper.update(setmeal);
              }
          }
        }
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    /*
        根据分类id查询菜品
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)  // 查询起售菜品
                .build();
        List<Dish> list = dishMapper.list(dish);
        return list;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
